#include <std.h>
#include <csl.h>
#include <csl_dat.h>
#include <csl_cache.h>
#include <chan.h>
#include <scom.h>
#include <utl.h>
#include <fvid.h>
#include <tsk.h>

#include <edc.h>
#include <vport.h>
#include <vportcap.h>
#include <tvp51xx.h>    

#include <evmdm642.h>
#include <evmdm642_capParamsSDTVDefault.h>


#include "appMain.h"
#include "appBiosObjects.h"
#include "appThreads.h"
#include "cap_dis_size.h"

VPORT_PortParams EVMDM642_vCapParamsPort 
  = EVMDM642_CAP_PARAMS_PORT_EMBEDDED_DEFAULT;

#if defined _NTSC

static VPORTCAP_Params EVMDM642_vCapParamsChan 
  = EVMDM642_CAP_PARAMS_CHAN_EMBEDDED_DEFAULT(NTSC720);

static TVP51XX_ConfParams EVMDM642_vCapParamsTVP5146 
  = EVMDM642_CAP_PARAMS_TVP51XX_EMBEDDED_DEFAULT(NTSC601, COMPOSITE, 0);

#elif defined _PAL

static VPORTCAP_Params EVMDM642_vCapParamsChan 
  = EVMDM642_CAP_PARAMS_CHAN_EMBEDDED_DEFAULT(PAL720);

static TVP51XX_ConfParams EVMDM642_vCapParamsTVP5146 
  = EVMDM642_CAP_PARAMS_TVP51XX_EMBEDDED_DEFAULT(PAL601, COMPOSITE, 0);

#endif


static unsigned char YArray[LINE_SZ*NUM_LINES*2];
static unsigned char CbArray[(LINE_SZ*NUM_LINES*2)>>1];
static unsigned char CrArray[(LINE_SZ*NUM_LINES*2)>>1];

/* heap IDs defined in the BIOS configuration file */
extern int extHeap;


FVID_Handle  capChan;
FVID_Frame *capFrameBuf;
void tskVideoInputInit()
{

    int             status;

    EVMDM642_vCapParamsChan.segId = extHeap;
    EVMDM642_vCapParamsTVP5146.hI2C = EVMDM642_I2C_hI2C;

    capChan = FVID_create("/VP0CAPTURE/A/0",
        IOM_INPUT, &status, (Ptr)&EVMDM642_vCapParamsChan, NULL);

    FVID_control(capChan, VPORT_CMD_EDC_BASE+EDC_CONFIG, (Ptr)&EVMDM642_vCapParamsTVP5146);
}

void tskVideoInputStart()
{
    FVID_control(capChan, VPORT_CMD_START, NULL);
}

/* handle video capture, scaling and re-format */
void tskVideoInput()
{
    int frame=0;
    SCOM_Handle fromInputtoProc,fromProctoInput;
    char *outBuf[3];
    char *inBuf[3];
    ScomMessage scomMsg;
    ScomMessage *pMsgBuf;

    fromInputtoProc = SCOM_open("INTOPROC");
    fromProctoInput = SCOM_open("PROCTOIN");

    FVID_alloc(capChan, &capFrameBuf);

    pMsgBuf = &scomMsg;

    while(1)
    {
        frame++;

        UTL_stsStart( stsCapTime );

        inBuf[0]  = capFrameBuf->frame.iFrm.y1;
        inBuf[1]  = capFrameBuf->frame.iFrm.cb1;
        inBuf[2]  = capFrameBuf->frame.iFrm.cr1;

        outBuf[0] =  (char *)YArray;
        outBuf[1] =  (char *)CbArray;
        outBuf[2] =  (char *)CrArray;

        yuv422to420(inBuf,outBuf, LINE_SZ, NUM_LINES);

        UTL_stsStop( stsCapTime );

        pMsgBuf->bufY = YArray;
        pMsgBuf->bufU = CbArray;
        pMsgBuf->bufV = CrArray;
        SCOM_putMsg(fromInputtoProc, pMsgBuf);

        // Calculate how long it takes to capture one frame
        UTL_stsPeriod( stsCycleTime );

        FVID_exchange(capChan, &capFrameBuf);

        pMsgBuf = SCOM_getMsg(fromProctoInput, SYS_FOREVER);
    }
}

